package com.ocm.tourism.core.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.OneToMany;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable
@Entity
public class CulinaryCulture extends Business {

   @Lob
   @org.hibernate.annotations.Type(type = "org.hibernate.type.TextType")
   private String content;

   @OneToMany(mappedBy="culinaryCulture", cascade=CascadeType.ALL, orphanRemoval=true)
   private List<Distance> distances = new ArrayList<Distance>();

   public String getContent() {
      return content;
   }

   public void setContent(String content) {
      this.content = content;
   }

   public List<Distance> getDistances() {
      return distances;
   }

   public void setDistances(List<Distance> distances) {
      this.distances = distances;
   }

   public String toString() {
      return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
   }
   
   public void addAndUpdateDistances(List<Distance> distances){
      for (Distance distance : distances){
         if (distance.getId() == null || distance.getId() == 0){
            addDistance(distance);
         }else{
            updateDistance(distance);
         }
      }
   }

   public void addDistance(Distance distance){
      distance.setCulinaryCulture(this);
      distances.size();
      distances.add(distance);
   }

   public void updateDistance(Distance distance){
      int index = this.distances.indexOf(distance);
      distance.setCulinaryCulture(this);
      distances.set(index, distance);
   }
}
